//
//  JWTMyCourseRecodesTableView.m
//  JWT
//
//  Created by ndm on 17/2/21.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTMyCourseRecodesTableView.h"
#import "JWTCourseTableViewCell.h"
#import "JWTListCourse.h"
#import "JWTStoreCourseDetailViewController.h"

static NSString *CourseTableViewCel_identifier = @"CourseTableViewCel_identifier";

@interface JWTMyCourseRecodesTableView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSArray *myReordArray;

@end

@implementation JWTMyCourseRecodesTableView

-(instancetype)initWithFrame:(CGRect)frame{
    self  = [super initWithFrame:frame];
    if (self) {
        [self initWithTableView];
        
        _myReordArray = [[NSArray alloc] init];
        
        [self reloadData];
    }
    return self;
}

-(void) initWithTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT -getNAVI_TOP_HEIGHT())];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [_tableView registerClass:[JWTCourseTableViewCell class] forCellReuseIdentifier:CourseTableViewCel_identifier];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_tableView];
}

#pragma mark - 表格视图数据源代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _myReordArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JWTCourseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CourseTableViewCel_identifier forIndexPath:indexPath];
    JWTListCourse *listCourse = [JWTListCourse mj_objectWithKeyValues:[_myReordArray objectAtIndex:indexPath.row]];
    [cell.courseImageView  sd_setImageWithURL:[NSURL URLWithString:listCourse.picture] placeholderImage:[UIImage imageNamed:ImageViewDefault]];
    cell.courseNameLabel.text = listCourse.coursename;
    cell.coachNameLabel.text = [NSString stringWithFormat:@"教练:%@",listCourse.coachName];
    
    cell.coursePriceLabel.text = [NSString stringWithFormat:@"¥%ld/课",listCourse.price];;
    cell.coursePersonLabel.text = [NSString stringWithFormat:@"(%ld/%ld人)",listCourse.ordernum,listCourse.totalnum];
    
    cell.courseTimeLabel.text = [NSString stringWithFormat:@"%@~%@",listCourse.starttime == nil?@"":[listCourse.starttime substringWithRange:NSMakeRange(5, 11)],listCourse.endtime == nil?@"":[listCourse.endtime substringWithRange:NSMakeRange(5, 11)]];
    
    //
    JWTDateString *dateString = [JWTDateString sharedInstance];
    NSString *cur_date_string = [dateString stringFromDate:[NSDate date]];
    if ([dateString compareSmallerTwoDate:cur_date_string Other:listCourse.starttime] == YES) {
        cell.courseStateLabel.text = @"即将开始";
    }else if ([dateString compareSmallerTwoDate:cur_date_string Other:listCourse.endtime] == NO){
        cell.courseStateLabel.text = @"已结束";
    }else{
        cell.courseStateLabel.text = @"进行中";
    }
    cell.courseStateLabel.textAlignment = NSTextAlignmentCenter;

    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JWTStoreCourseDetailViewController *vc = [[JWTStoreCourseDetailViewController alloc] init];
    UITabBarController *tabBar = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    UINavigationController *nav = tabBar.selectedViewController;
    JWTListCourse *listCourse = [JWTListCourse mj_objectWithKeyValues:[_myReordArray objectAtIndex:indexPath.row]];
    vc.courseID = listCourse.courseid;
    [nav pushViewController:vc animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    static JWTCourseTableViewCell *cell = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cell = [tableView dequeueReusableCellWithIdentifier:CourseTableViewCel_identifier];
    });
    return cell.height;
}

/**
 请求我的预约记录
 */
-(void) reloadData{
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] getJWTMyCourseRecord];
    [HUD showUIBlockingIndicator];
    [AFNHttpsRequest requestWithUrl:urlString WithParam:nil WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [HUD hideUIBlockingIndicator];
        if (model) {
            if ([[model objectForKey:@"code"]integerValue] == 200) {
                NSLog(@"%@",model);
                _myReordArray = [[model objectForKey:@"data"] objectForKey:@"list"];
                [_tableView reloadData];
            }
        }
    }];
    
}


@end
